home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_096 / animplayer / pack_frame.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  222 lines

  1. /***************************************************************************
  2.  *
  3.  *   NAME 
  4.  *      Pack_Frame -- store packed frame to disk
  5.  *
  6.  *   SYNOPSIS
  7.  *      if (!Pack_Frame( file, bitmapold, bitmapnew, colormap ))
  8.  *
  9.  *    BPTR file;
  10.  *      struct BitMap *bitmapold, *bitmapnew;
  11.  *      UWORD *colormap;
  12.  *
  13.  *   DESCRIPTION
  14.  *      Packs an image that is part of an animation in RAM
  15.  *      by saving the XOR of every other frame
  16.  *      and exploiting frame coherence. 
  17.  *
  18.  *      copyright (c) 1987 Martin D. Hash
  19.  *
  20.  *   LAST EDITED
  21.  *      Martin Hash            22 Aug 1987
  22.  *
  23.  *   EDIT HISTORY
  24.  *      25 Mar 1987  MH  Created.
  25.  *      22 May         Changed df1: to frames:
  26.  *       9 Jun             Speeded up playback.
  27.  *    17 Jul         Disk Full.
  28.  *
  29.  **********************************************************************/
  30.  
  31. #include <exec/types.h>
  32. #include <intuition/intuition.h>
  33. #include <libraries/dos.h>
  34. #include <libraries/dosextens.h>
  35. #include "df1:FileCons.h"
  36. #include "df1:ANIMCons.h"
  37.  
  38. /* EXTERNAL FUNCTIONS */
  39.  
  40. BOOL UserRequest();
  41.  
  42. /* LOCAL CONSTANTS */
  43.  
  44. #define Abort()     goto Write_Error_Exit
  45.  
  46. /* FILE VARIABLES */
  47.  
  48. static int sizedummy1, sizedummy2;
  49. static UWORD *dummy2,
  50.              *dummy1; 
  51.  
  52. /* FUNCTION */
  53.  
  54. BOOL Pack_Frame( file, bitmapold, bitmapnew, colormap )
  55.  
  56. BPTR file;
  57. struct BitMap *bitmapold, *bitmapnew;
  58. UWORD *colormap;
  59. {
  60.    /* LOCAL VARIABLES */
  61.  
  62.    UWORD *offsetptr,
  63.      value,
  64.          *ptr1, *ptr2,
  65.          *position,
  66.          *dataptr;
  67.    int width, 
  68.        plane,
  69.        i,
  70.        number,
  71.        maxrows, 
  72.        column, row;
  73.    ULONG data[8],
  74.          offset[8],
  75.      firstpointer = POINTERBLOCK,
  76.      CMAPsize,
  77.      FORMsize,
  78.      DLTAsize;
  79.    BOOL done,
  80.     first;
  81.    ColorReg colorreg;
  82.    
  83.    /* CODE */
  84.  
  85.    sizedummy1 = bitmapnew->BytesPerRow/sizeof(UWORD)
  86.     *bitmapnew->Rows*sizeof(UWORD);
  87.    if ((dummy1 = (UWORD *)AllocMem( sizedummy1, 0 )) == NULL) {
  88.       UserRequest( "      NOT ENOUGH MEMORY!" ); 
  89.       return FALSE;
  90.    }
  91.    sizedummy2 = bitmapold->BytesPerRow/sizeof(UWORD)
  92.     *bitmapold->Rows*sizeof(UWORD)*1.5;
  93.    if ((dummy2 = (UWORD *)AllocMem( sizedummy2, 0 )) == NULL) {
  94.       FreeMem( (UBYTE *)dummy1, sizedummy1 );
  95.       UserRequest( "      NOT ENOUGH MEMORY!" ); 
  96.       return FALSE;
  97.    }
  98.  
  99.    for (i = 0; i < 8; ++i) {
  100.       data[i] = 0;
  101.       offset[i] = 0;
  102.    }
  103.    width = bitmapold->BytesPerRow >>1;
  104.    maxrows = bitmapold->Rows;
  105.    dataptr = dummy1;
  106.    offsetptr = dummy2;
  107.    for (plane = 0; plane < bitmapold->Depth; ++plane) {
  108.       ptr1 = (UWORD *)bitmapold->Planes[plane];
  109.       ptr2 = (UWORD *)bitmapnew->Planes[plane];
  110.  
  111.       done = FALSE;
  112.       column = 1;
  113.       row = 1;
  114.       while (TRUE) {
  115.          while ((*ptr1 ^ *ptr2) == 0) {
  116.             ptr1 += width;
  117.             ptr2 += width;
  118.         if (++row > maxrows) {
  119.            row = 1;
  120.                ptr1 = (UWORD *)bitmapold->Planes[plane] + column;
  121.                ptr2 = (UWORD *)bitmapnew->Planes[plane] + column;
  122.            
  123.                if (++column > width) {
  124.               *offsetptr = 0xFFFF;
  125.                   done = TRUE;
  126.               break;
  127.            }
  128.             }
  129.          }
  130.      if (done) {
  131.         ++offsetptr;
  132.         break;
  133.          }
  134.            
  135.      first = TRUE;
  136.          number = 0;
  137.      while ((value = *ptr1 ^ *ptr2) != 0) {
  138.         *dataptr++ = value;    
  139.         if (first) {
  140.            first = FALSE;
  141.                position = ptr1;
  142.         }
  143.         ptr1 += width;
  144.         ptr2 += width;
  145.         ++number;
  146.         if (++row > maxrows) {
  147.            row = 1;
  148.                ptr1 = (UWORD *)bitmapold->Planes[plane] + column;
  149.                ptr2 = (UWORD *)bitmapnew->Planes[plane] + column;
  150.            if (++column > width) {
  151.           done = TRUE;
  152.               *(offsetptr+2) = 0xFFFF;
  153.            }
  154.            break;
  155.         }
  156.          }
  157.          *offsetptr++ = position - (UWORD *)bitmapold->Planes[plane];
  158.      *offsetptr++ = number;
  159.      if (done) {
  160.         ++offsetptr;
  161.         break;
  162.      }
  163.       }
  164.       data[plane] = POINTERBLOCK + dataptr - dummy1;
  165.       offset[plane] = offsetptr - dummy2;
  166.    }
  167.  
  168.    if (Write( file, "FORM", 4*sizeof(char)) == -1)
  169.       Abort();
  170.    DLTAsize = (data[plane-1] + offset[plane-1]) * sizeof(UWORD);
  171.    FORMsize = (HEADERSIZE*sizeof(char) + HEADERSIZE*sizeof(char) 
  172.     + sizeof(ULONG) + sizeof(ColorReg)*COLORS 
  173.     + HEADERSIZE*sizeof(char) + sizeof(ULONG) + DLTAsize);
  174.    if (Write( file, &FORMsize, sizeof(ULONG)) == -1)
  175.       Abort();
  176.    if (Write( file, "ILBMCMAP", 8*sizeof(char)) == -1)
  177.       Abort();
  178.    CMAPsize = sizeof(ColorReg)*COLORS;
  179.    if (Write( file, &CMAPsize, sizeof(ULONG)) == -1)
  180.       Abort();
  181.    for (i = 0; i < COLORS; ++i) {
  182.       colorreg.red = (*colormap>>4) & 0xF0;
  183.       colorreg.green = *colormap & 0xF0;
  184.       colorreg.blue = (*colormap<<4) & 0xF0;
  185.       if (Write( file, (UBYTE *)&colorreg, sizeof(ColorReg)) == -1) 
  186.      Abort();
  187.       ++colormap;
  188.    }
  189.  
  190.    if (Write( file, "DLTA", 4*sizeof(char)) == -1)
  191.       Abort();
  192.    if (Write( file, &DLTAsize, sizeof(ULONG)) == -1)
  193.       Abort();
  194.  
  195.    for (i = 0; i < plane; ++i)
  196.       offset[i] += data[plane-1];
  197.    if (Write( file, &firstpointer, sizeof(ULONG)) == -1)
  198.       Abort();
  199.    if (Write( file, &data[0], 7*sizeof(ULONG)) == -1)
  200.       Abort();
  201.    if (Write( file, &data[plane-1], sizeof(ULONG)) == -1)
  202.       Abort();
  203.    if (Write( file, &offset[0], 7*sizeof(ULONG)) == -1)
  204.       Abort();
  205.    if (Write( file, dummy1, (data[plane-1]-POINTERBLOCK)*sizeof(UWORD))== -1)
  206.       Abort();
  207.    if (Write( file, dummy2, (offset[plane-1]-data[plane-1])*sizeof(UWORD))
  208.     == -1)
  209.       Abort();
  210.     
  211.    FreeMem((UBYTE *)dummy1, sizedummy1 );
  212.    FreeMem((UBYTE *)dummy2, sizedummy2 );
  213.    return TRUE;
  214.  
  215. Write_Error_Exit:
  216.  
  217.    FreeMem((UBYTE *)dummy1, sizedummy1 );
  218.    FreeMem((UBYTE *)dummy2, sizedummy2 );
  219.    return FALSE;
  220. }
  221.  
  222.